package com.leetcode.图.图的同构;

/**
 * 图的定义
 * @author LZF
 *
 */
public class Graph {
	//邻接矩阵
	private int[][] matrix;
	//节点个数
	private int NodesSize;
	//统计度数表：节点的度表,i表示度，totalDegrees[i]表示度为i的节点有多少个
	private int[] totalDegrees;
	//度数表：每个节点的度数，nodeOfSDegree[i - 1]表示节点i的度数为多少
	private int[] nodeOfDegree;
	
	public Graph() {
		
	}
	
	public Graph(int size) {
		this.NodesSize = size;
		this.matrix = new int[size][size];
	}
	
	public Graph(int[][] matrix) {
		this.matrix = matrix;
		this.NodesSize = matrix.length;
	}
	
	public void setNodesSize(int size) {
		this.NodesSize = size;
		this.matrix = new int[size][size];
	}
	
	public int getNodesSize() {
		return this.NodesSize;
	}
	
	public Graph addEdge(int node1,int node2) {
		if(node1 == node2) return this;
//		try {
			matrix[node1 - 1][node2 - 1] = 1;
			matrix[node2 - 1][node1 - 1] = 1;
//		} catch (IndexOutOfBoundsException e) {
////			System.out.println("!!输入的顶点有误!!请输入顶点为1到" + this.NodesSize + "之间的整数!!");
//			e.printStackTrace();
//		}
		return this;
	}
	
	//节点的度表,i表示度，degreesSize[i]表示度为i的节点有多少个
	public int[] getTotalDegrees() {
		if(this.totalDegrees == null) {
			this.totalDegrees = new int[this.NodesSize];
			for(int[] arr : this.matrix) {
				int d = 0;
				for(int i = 0;i < arr.length;i++) {
					if(arr[i] == 1) d++;
				}
				this.totalDegrees[d]++;
			}
		}
		return this.totalDegrees;
	}
	
	//每个节点的度数，nodeOfSDegree[i - 1]表示节点i的度数为多少
	public int[] getNodeOfDegree() {
		if(this.nodeOfDegree == null) {
			this.nodeOfDegree = new int[this.NodesSize];
			for(int i = 0;i < NodesSize;i++) {
				for(int j = 0;j < NodesSize;j++) {
					if(this.matrix[i][j] == 1) nodeOfDegree[i]++;
				}
			}
		}
		return this.nodeOfDegree;
	}
	
	public int[][] getMatrix(){
		return this.matrix;
	}
}
